#include "Game.h"
#include "Map.h"
#include <math.h>

extern short sintable[1024];

void StartGame(Game *game,struct Map *map)
{
	RestartGame(game,map);
	game->frame=0;
	game->eventtype=GameStartedEvent;
	game->eventtime=0;
}

void RestartGame(Game *game,struct Map *map)
{
	game->x=(map->start_x<<12)+0x800;
	game->y=(map->start_y<<12)+0x800;
	game->z=MapRoof(map,map->start_x,map->start_y);
	game->vx=game->vy=game->vz=0;
	game->a=map->start_angle;
	game->eventtype=GameRestartedEvent;
	game->eventtime=game->frame;
	game->gate=0;
	game->timer=0;
	game->lasttime=0;
	game->lasthit=-10000;
}

void GameLoop(Game *game,struct Map *map,int left,int right,int up,int down,int run,int crouch)
{
	game->frame++;

	if(left) game->a=(game->a-32)&0x3ff;
	if(right) game->a=(game->a+32)&0x3ff;

	int crouchheight=100;
	if(!crouch) crouchheight=400;
	game->vz-=10;

	game->vx=((game->vx<<4)-game->vx)/16;
	game->vy=((game->vy<<4)-game->vy)/16;
	game->vz=((game->vz<<4)-game->vz)/16;

	int curr_x=game->x>>12;
	int curr_y=game->y>>12;
	int curr_floor=MapFloor(map,curr_x,curr_y);
	int curr_roof=MapRoof(map,curr_x,curr_y);

	moveloop:
	game->x=(game->x+game->vx)%(map->w*0x1000);
	game->y=(game->y+game->vy)%(map->h*0x1000);
	game->z=game->z+game->vz;

	int new_x=game->x>>12;
	int new_y=game->y>>12;

	int new_floor=MapFloor(map,new_x,new_y);
	int new_roof=MapRoof(map,new_x,new_y);

	if((new_floor>curr_floor && game->z<new_floor)
	||(new_roof<curr_roof && game->z>new_roof))
	{
		if(curr_x==new_x)
		{
			game->y-=game->vy;
			game->vy=-game->vy/2;
		}
		else
		{
			game->x-=game->vx;
			game->vx=-game->vx/2;

			if(curr_y!=new_y)
			{
				game->y-=game->vy;
				game->vy=-game->vy/2;
			}
		}
		game->lasthit=game->frame;
		goto moveloop;
	}

	if(game->z<new_floor+crouchheight)
	{
		if(game->z<new_floor)
		{
			game->z=new_floor;
			game->vz=0;
		}
		game->vz+=(new_floor+crouchheight-game->z)/4;

		if((up||down)&&!(up&&down))
		{
			int divisor=64;
			if(run) divisor=32;
			if(down) divisor=-divisor;

			game->vx+=sintable[(game->a+256)&1023]/divisor;
			game->vy+=sintable[game->a&1023]/divisor;
//			game->vx+=((int)4096*cos((double)game->a/1024.0*2*M_PI))/divisor;
//			game->vy+=((int)4096*sin((double)game->a/1024.0*2*M_PI))/divisor;
		}
	}

	if(game->z>new_roof)
	{
		game->z=new_roof;
		game->vz=0;
	}

	int new_gate=MapGate(map,new_x,new_y);

	if(game->gate)
	{
		game->timer++;

		if(game->gate==4&&new_gate==1)
		{
			game->eventtype=GameLapEvent;
			game->eventtime=game->frame;
			game->lasttime=game->timer;
			game->timer=0;
			game->gate=1;
		}
		else if(new_gate==game->gate+1)
		{
			game->eventtype=GameGateEvent;
			game->eventtime=game->frame;
			game->gate++;
		}

		if(game->timer>=100*25)
		{
			game->eventtype=GameStoppedRaceEvent;
			game->eventtime=game->frame;
			game->timer=0;
			game->gate=0;
		}
	}
	else if(new_gate==1)
	{
		game->eventtype=GameGateEvent;
		game->eventtime=game->frame;
		game->timer=0;
		game->gate=1;
	}
}

short sintable[1024]=
{
	0,25,50,75,100,125,150,175,
	200,226,251,276,301,326,351,376,
	401,426,451,476,501,526,551,576,
	601,625,650,675,700,725,749,774,
	799,823,848,872,897,921,946,970,
	995,1019,1043,1068,1092,1116,1140,1164,
	1189,1213,1237,1260,1284,1308,1332,1356,
	1379,1403,1427,1450,1474,1497,1520,1544,
	1567,1590,1613,1636,1659,1682,1705,1728,
	1751,1773,1796,1819,1841,1864,1886,1908,
	1930,1952,1975,1997,2018,2040,2062,2084,
	2105,2127,2148,2170,2191,2212,2233,2254,
	2275,2296,2317,2337,2358,2379,2399,2419,
	2439,2460,2480,2500,2519,2539,2559,2578,
	2598,2617,2637,2656,2675,2694,2713,2732,
	2750,2769,2787,2806,2824,2842,2860,2878,
	2896,2914,2931,2949,2966,2983,3000,3018,
	3034,3051,3068,3085,3101,3117,3134,3150,
	3166,3182,3197,3213,3229,3244,3259,3274,
	3289,3304,3319,3334,3348,3363,3377,3391,
	3405,3419,3433,3447,3460,3473,3487,3500,
	3513,3526,3538,3551,3563,3576,3588,3600,
	3612,3624,3635,3647,3658,3669,3680,3691,
	3702,3713,3723,3734,3744,3754,3764,3774,
	3784,3793,3803,3812,3821,3830,3839,3848,
	3856,3864,3873,3881,3889,3897,3904,3912,
	3919,3926,3933,3940,3947,3954,3960,3967,
	3973,3979,3985,3990,3996,4001,4007,4012,
	4017,4022,4026,4031,4035,4039,4043,4047,
	4051,4055,4058,4062,4065,4068,4071,4073,
	4076,4078,4080,4082,4084,4086,4088,4089,
	4091,4092,4093,4094,4094,4095,4095,4095,
	4096,4095,4095,4095,4094,4094,4093,4092,
	4091,4089,4088,4086,4084,4082,4080,4078,
	4076,4073,4071,4068,4065,4062,4058,4055,
	4051,4047,4043,4039,4035,4031,4026,4022,
	4017,4012,4007,4001,3996,3990,3985,3979,
	3973,3967,3960,3954,3947,3940,3933,3926,
	3919,3912,3904,3897,3889,3881,3873,3864,
	3856,3848,3839,3830,3821,3812,3803,3793,
	3784,3774,3764,3754,3744,3734,3723,3713,
	3702,3691,3680,3669,3658,3647,3635,3624,
	3612,3600,3588,3576,3563,3551,3538,3526,
	3513,3500,3487,3473,3460,3447,3433,3419,
	3405,3391,3377,3363,3348,3334,3319,3304,
	3289,3274,3259,3244,3229,3213,3197,3182,
	3166,3150,3134,3117,3101,3085,3068,3051,
	3034,3018,3000,2983,2966,2949,2931,2914,
	2896,2878,2860,2842,2824,2806,2787,2769,
	2750,2732,2713,2694,2675,2656,2637,2617,
	2598,2578,2559,2539,2519,2500,2480,2460,
	2439,2419,2399,2379,2358,2337,2317,2296,
	2275,2254,2233,2212,2191,2170,2148,2127,
	2105,2084,2062,2040,2018,1997,1975,1952,
	1930,1908,1886,1864,1841,1819,1796,1773,
	1751,1728,1705,1682,1659,1636,1613,1590,
	1567,1544,1520,1497,1474,1450,1427,1403,
	1379,1356,1332,1308,1284,1260,1237,1213,
	1189,1164,1140,1116,1092,1068,1043,1019,
	995,970,946,921,897,872,848,823,
	799,774,749,725,700,675,650,625,
	601,576,551,526,501,476,451,426,
	401,376,351,326,301,276,251,226,
	200,175,150,125,100,75,50,25,
	0,-25,-50,-75,-100,-125,-150,-175,
	-200,-226,-251,-276,-301,-326,-351,-376,
	-401,-426,-451,-476,-501,-526,-551,-576,
	-601,-625,-650,-675,-700,-725,-749,-774,
	-799,-823,-848,-872,-897,-921,-946,-970,
	-995,-1019,-1043,-1068,-1092,-1116,-1140,-1164,
	-1189,-1213,-1237,-1260,-1284,-1308,-1332,-1356,
	-1379,-1403,-1427,-1450,-1474,-1497,-1520,-1544,
	-1567,-1590,-1613,-1636,-1659,-1682,-1705,-1728,
	-1751,-1773,-1796,-1819,-1841,-1864,-1886,-1908,
	-1930,-1952,-1975,-1997,-2018,-2040,-2062,-2084,
	-2105,-2127,-2148,-2170,-2191,-2212,-2233,-2254,
	-2275,-2296,-2317,-2337,-2358,-2379,-2399,-2419,
	-2439,-2460,-2480,-2500,-2519,-2539,-2559,-2578,
	-2598,-2617,-2637,-2656,-2675,-2694,-2713,-2732,
	-2750,-2769,-2787,-2806,-2824,-2842,-2860,-2878,
	-2896,-2914,-2931,-2949,-2966,-2983,-3000,-3018,
	-3034,-3051,-3068,-3085,-3101,-3117,-3134,-3150,
	-3166,-3182,-3197,-3213,-3229,-3244,-3259,-3274,
	-3289,-3304,-3319,-3334,-3348,-3363,-3377,-3391,
	-3405,-3419,-3433,-3447,-3460,-3473,-3487,-3500,
	-3513,-3526,-3538,-3551,-3563,-3576,-3588,-3600,
	-3612,-3624,-3635,-3647,-3658,-3669,-3680,-3691,
	-3702,-3713,-3723,-3734,-3744,-3754,-3764,-3774,
	-3784,-3793,-3803,-3812,-3821,-3830,-3839,-3848,
	-3856,-3864,-3873,-3881,-3889,-3897,-3904,-3912,
	-3919,-3926,-3933,-3940,-3947,-3954,-3960,-3967,
	-3973,-3979,-3985,-3990,-3996,-4001,-4007,-4012,
	-4017,-4022,-4026,-4031,-4035,-4039,-4043,-4047,
	-4051,-4055,-4058,-4062,-4065,-4068,-4071,-4073,
	-4076,-4078,-4080,-4082,-4084,-4086,-4088,-4089,
	-4091,-4092,-4093,-4094,-4094,-4095,-4095,-4095,
	-4096,-4095,-4095,-4095,-4094,-4094,-4093,-4092,
	-4091,-4089,-4088,-4086,-4084,-4082,-4080,-4078,
	-4076,-4073,-4071,-4068,-4065,-4062,-4058,-4055,
	-4051,-4047,-4043,-4039,-4035,-4031,-4026,-4022,
	-4017,-4012,-4007,-4001,-3996,-3990,-3985,-3979,
	-3973,-3967,-3960,-3954,-3947,-3940,-3933,-3926,
	-3919,-3912,-3904,-3897,-3889,-3881,-3873,-3864,
	-3856,-3848,-3839,-3830,-3821,-3812,-3803,-3793,
	-3784,-3774,-3764,-3754,-3744,-3734,-3723,-3713,
	-3702,-3691,-3680,-3669,-3658,-3647,-3635,-3624,
	-3612,-3600,-3588,-3576,-3563,-3551,-3538,-3526,
	-3513,-3500,-3487,-3473,-3460,-3447,-3433,-3419,
	-3405,-3391,-3377,-3363,-3348,-3334,-3319,-3304,
	-3289,-3274,-3259,-3244,-3229,-3213,-3197,-3182,
	-3166,-3150,-3134,-3117,-3101,-3085,-3068,-3051,
	-3034,-3018,-3000,-2983,-2966,-2949,-2931,-2914,
	-2896,-2878,-2860,-2842,-2824,-2806,-2787,-2769,
	-2750,-2732,-2713,-2694,-2675,-2656,-2637,-2617,
	-2598,-2578,-2559,-2539,-2519,-2500,-2480,-2460,
	-2439,-2419,-2399,-2379,-2358,-2337,-2317,-2296,
	-2275,-2254,-2233,-2212,-2191,-2170,-2148,-2127,
	-2105,-2084,-2062,-2040,-2018,-1997,-1975,-1952,
	-1930,-1908,-1886,-1864,-1841,-1819,-1796,-1773,
	-1751,-1728,-1705,-1682,-1659,-1636,-1613,-1590,
	-1567,-1544,-1520,-1497,-1474,-1450,-1427,-1403,
	-1379,-1356,-1332,-1308,-1284,-1260,-1237,-1213,
	-1189,-1164,-1140,-1116,-1092,-1068,-1043,-1019,
	-995,-970,-946,-921,-897,-872,-848,-823,
	-799,-774,-749,-725,-700,-675,-650,-625,
	-601,-576,-551,-526,-501,-476,-451,-426,
	-401,-376,-351,-326,-301,-276,-251,-226,
	-200,-175,-150,-125,-100,-75,-50,-25
};
